

jQuery.ffui = {
	/**
	 * 创建一个dom，如果已存在就选择这个dom ,并返回jq对象
	 * 
	 * @param {}
	 *            id 标签id
	 * @param {}
	 *            tag 标签名称
	 * @param {}
	 *            attr 属性json
	 * @return {} jq dom
	 */
	getId : function(id, tag, attr) {
		var $obj = $("#" + id);
		var attrStr = "";

		if ($obj.size() == 0) {
			if (attr) {
				$.each(attr, function(key, val) {
							attrStr += key + "=" + val + " ";
						});
			}
			if (!tag) {
				tag = "div";
			}
			$obj = $("<" + tag + " id='" + id + "' " + attrStr + " ></" + tag
					+ ">");
			$("body").append($obj);
		}
		return $obj;
	},
	/**
	 * 创建一个遮罩层
	 * 
	 * @return {遮罩层jq对象}
	 */
	mask : function(id, color) {
		if (!id) {
			id = "ffui-mask";
		}
		if (!color) {
			color = "#000000";
		}

		var $mask = $.ffui.getId(id);
		var $body = $("body");

		$mask.css({
					opacity : 0.50,
					filter : "alpha(opacity=50)",
					"background-color" : color,
					"z-index" : 198,
					position : "absolute",
					width : "100%",
					height : $(document).height(),
					top : 0,
					left : 0,
					display : "none"
				});
		return $mask;
	},
	slider : function(options) {

		var o = {
			type : 0,
			btnLeft : "",
			btnRight : "",
			scBox : "",
			step : 200,
			timer : 500,
			count : 4,
			showCount : 4,
			isTurn : true,
			top : 0,
			left : 0,
			startCount : 0,
			autoPlay : true
		};
		$.extend(o, options);

		var isOver = true;

		$("#" + o.btnLeft).click(function() {

					if (isOver) {
						stopPlay();
						move(false);
					}
					return false;
				});

		$("#" + o.btnRight).click(function() {

					if (isOver) {
						stopPlay();
						move(true);
					}

					return false;
				});
		var $scBox = $("#" + o.scBox);
		var nowCount = o.startCount;
		var left = o.left;
		var top = o.top;
		var posStr = "";
		$scBox.css({
					left : left,
					top : top
				});
		// 自动补足个数
		$scBox.append($scBox.children().clone());

		var move = function(flag) {
			isOver = false;
			if (o.type == 1) {
				posStr = "top";
				if (!flag) {
					if (o.isTurn) {
						if (nowCount == 0) {
							nowCount = o.count;
							$scBox.css(posStr, -(nowCount * o.step) + "px");
						}
						top = -(nowCount - 1) * o.step;
					} else {
						if (nowCount != 0) {
							top = -(nowCount - 1) * o.step;
						}
					}
				} else {
					if (o.isTurn
							|| (!o.isTurn && (nowCount != o.count - o.showCount))) {
						top = -(nowCount + 1) * o.step;
					}
				}
			} else {
				posStr = "left";
				if (!flag) {
					if (o.isTurn) {
						if (nowCount == 0) {
							nowCount = o.count;
							$scBox.css(posStr, -(nowCount * o.step) + "px");
						}
						left = -(nowCount - 1) * o.step;
					} else {
						if (nowCount != 0) {
							left = -(nowCount - 1) * o.step;
						}
					}
				} else {
					if (o.isTurn
							|| (!o.isTurn && (nowCount != o.count - o.showCount))) {
						left = -(nowCount + 1) * o.step;
					}
				}
			}
			$scBox.animate({
						left : left + "px",
						top : top + "px"
					}, {
						duration : o.timer,
						complete : function() {
							if (flag) {
								nowCount += 1;
								if (o.isTurn) {
									if (nowCount > (o.count - 1)) {
										nowCount = 0;
										$scBox.css(posStr, "0px");
									}
								} else {
									if ((nowCount + o.showCount) > o.count) {
										nowCount -= 1;
									}
								}
							} else {
								if (o.isTurn || (!o.isTurn && nowCount != 0)) {
									nowCount -= 1;
								}
							}
							isOver = true;

							autoPlay();

						}
					});
		};

		// auto
		var autoTimer = "";
		var autoPlay = function() {
			if (o.autoPlay) {
				autoTimer = setTimeout(function() {
							move(true);
						}, 3000);
			}
		};
		var stopPlay = function() {
			if (o.autoPlay) {
				clearTimeout(autoTimer);
			}
		};

		autoPlay();

	},
	birthday : function(options, setting) {
		var o = {
			yid : "yid",
			mid : "mid",
			did : "did",
			isTip : true,
			change : function() {
			}
		};

		$.extend(o, options);
		var date = new Date();

		var $selYear = $("#" + o.yid);
		var $selMonth = $("#" + o.mid);
		var $selDay = $("#" + o.did)

		var y = 0, m = 0, d = 0;

		var createYear = function() {
			var html = "";
			if (o.isTip) {
				html += "<option value=''>请选择</option>";
			}

			for (var i = 1994; i >= 1952; i--) {
				html += "<option value='" + i + "'>" + i + "年</option>";
			}
			$selYear.html(html);

		};

		var createMonth = function() {
			var html = "";
			if (o.isTip) {
				html += "<option value=''>请选择</option>";
			}
			for (var i = 1; i <= 12; i++) {
				html += "<option value='" + i + "'>" + i + "月</option>";
			}
			$selMonth.html(html);

		};

		var createDay = function(y, m) {
			if (y != "" && m != "" && y != 0 && m != 0) {
				var days = 0;
				if (m == 1 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
					days = 31;
				} else if (m != 2) {
					days = 30;
				} else {
					if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
						days = 29;
					} else {
						days = 28;
					}
				}

				var html = "";
				if (o.isTip) {
					html += "<option value=''>请选择</option>";
				}
				for (var i = 1; i <= days; i++) {
					html += "<option value='" + i + "'>" + i + "日</option>";
				}

				$selDay.html(html);

			}
		};

		var isSameType = function(m1, m2) {
			var monthType = {
				1 : [1, 5, 7, 8, 10, 12],
				2 : [2],
				3 : [3, 4, 6, 9, 11]
			};
			var m1Type = -1;
			var m2Type = -1;
			$.each(monthType, function(key, val) {
						$(val).each(function(i, d) {
									if (m1 == d) {
										m1Type = key;
									}
									if (m2 == d) {
										m2Type = key;
									}
								});

					});

			return m1Type == m2Type;
		};

		$selYear.change(function() {
					if ($selMonth.val() == "2") {
						createDay($selYear.val(), $selMonth.val());
					}

					o.change($selYear.val(), $selMonth.val(), $selDay.val());
					y = parseInt($selYear.val());
				});
		$selMonth.change(function() {
					if (!isSameType(m, parseInt($selMonth.val()))) {
						createDay($selYear.val(), $selMonth.val());
						o
								.change($selYear.val(), $selMonth.val(),
										$selDay.val());
						m = parseInt($selMonth.val());
					}
				});
		$selDay.change(function() {
					o.change($selYear.val(), $selMonth.val(), $selDay.val());
				});

		createYear();
		createMonth();
		createDay(y, m);

		// 设置日期

		var s = {
			y : "",
			m : "",
			d : ""
		};
		$.extend(s, setting);

		setTimeout(function() {
					$selYear.val(s.y).change();
				}, 20);
		setTimeout(function() {
					$selMonth.val(s.m).change();
				}, 20);

		setTimeout(function() {
					$selDay.val(s.d).change();
				}, 50);

	}
};

(function($, undefined) {

	$.fn.cityMenu = function(options) {

		$.fn.cityMenu.init();

		// 定位偏移量

		$(this).click(function() {
					$.extend($.fn.cityMenu.defaults, options);
					$.fn.cityMenu.$input = $(this);
					$.fn.cityMenu.showMenu(this);
					// 阻止冒泡防止被关闭
					return false;
				});

		return this;
	};

	$.fn.cityMenu.defaults = {
		next : function() {
		},
		closeFn : function() {
		}
	};

	// 公有变量
	$.fn.cityMenu.$cityMenu = "";
	// 省份id
	$.fn.cityMenu.pid = 0;
	$.fn.cityMenu.pname = "";
	// 城市id
	$.fn.cityMenu.cid = 0;
	$.fn.cityMenu.cname = "";
	// 单个选中对象
	$.fn.cityMenu.$input = "";

	// 载入地区数据
	$.fn.cityMenu.loadProvince = function(next) {
		$.get('/v2/js/ffui/data/province/province.js', function(data) {
					if (next) {
						var json = $.parseJSON(data);
						next(json);
					}
				});
	};

	// 载入城市数据
	$.fn.cityMenu.loadCity = function(id, next) {
		$.get("/v2/js/ffui/data/city/city_" + id + ".js", function(data) {
					if (next) {
						var json = $.parseJSON(data);
						next(json);
					}
				});
	};
	// 创建或找到menu主面板
	$.fn.cityMenu.init = function() {

		var t = this;
		this.$cityMenu = $("#ffui-city-menu");
		if (this.$cityMenu.size() == 0) {
			this.$cityMenu = $('<div id="ffui-city-menu" class="ffui-city-menu" ><div class="ffui-city-menu-p" ></div><div class="ffui-city-menu-c" ></div></div>');
			$("body").append(this.$cityMenu);
			// 载入数据
			t.loadProvince(function(jsonArr) {
						t.createProvince(jsonArr);
					});

		}

		// 关闭
		$(document).click(function(e) {
					t.$cityMenu.hide();
					t.defaults.closeFn();
				});
		t.$cityMenu.click(function() {
					return false;
				});
	};

	// 创建省份
	$.fn.cityMenu.createProvince = function(josnArr) {
		var t = this;
		var $p = t.$cityMenu.find("div.ffui-city-menu-p").addClass("remove-bg");
		var html = "";

		$(josnArr).each(function(i, json) {
			html += '<a href="#" id="ffui-city-menu-p_' + json.id + '" type="'
					+ json.type + '" >' + json.name + '</a>';
		});

		$p.html(html);
		$p.find("a").click(function() {
			t.pid = $(this).attr("id").split("_")[1];
			t.pname = $(this).html();
			// 重置城市名
			t.cid = 0;
			t.cname = "";

			$p.find("a").removeClass("here");
			$(this).addClass("here");

			// 判断是否为直辖市
			if ($(this).attr("type") != "-1") {
				// 显示进度条
				t.$cityMenu.find("div.ffui-city-menu-c")
						.removeClass("remove-bg");

				t.loadCity(t.pid, function(jsonArr) {
							t.createCity(jsonArr);
						});
			} else {
				t.$cityMenu.find("div.ffui-city-menu-c").hide();
				t.$cityMenu.hide();
				t.setInfo();

			}
			return false;
		});
	};

	// 创建地市
	$.fn.cityMenu.createCity = function(jsonArr) {
		var t = this;
		var $c = t.$cityMenu.find("div.ffui-city-menu-c").addClass("remove-bg");
		var html = "";
		$(jsonArr).each(function(i, json) {
			html += '<a href="#" id="ffui-city-menu-c_' + json.id + '"  >'
					+ json.name + '</a>';
		});
		$c.html(html).show();
		$c.find("a").click(function() {
					t.cid = $(this).attr("id").split("_")[1];
					t.cname = $(this).html();
					$c.find("a").removeClass("here");
					$(this).addClass("here");

					t.setInfo();

					t.$cityMenu.hide();
					// $mask.hide();
					return false;
				});
	};
	// 设置显示信息
	$.fn.cityMenu.setInfo = function() {
		if (this.cname != "") {
			this.cname = "-" + this.cname;
		}
		this.$input.val(this.pname + this.cname);
		this.defaults.next(this.pid, this.pname, this.cid, this.cname);
	};
	// 显示层
	$.fn.cityMenu.showMenu = function(input) {
		var offset = $(input).offset();
		this.$cityMenu.css({
					top : offset.top + $(input).outerHeight(true),
					left : offset.left,
					display : "block"
				});

	};

	// =======================================

	$.fn.schoolMenu = function(options) {

		$.fn.schoolMenu.init();

		$(this).click(function() {
					$.extend($.fn.schoolMenu.defaults, options)
					$.fn.schoolMenu.$input = $(this);
					$.fn.schoolMenu.showMenu(this);
					// 阻止冒泡防止被关闭
					return false;
				});
		return this;
	};

	$.fn.schoolMenu.defaults = {
		next : function() {
		},
		closeFn : function() {
		}
	};
	$.fn.schoolMenu.$schoolMenu = "";
	// 省份id
	$.fn.schoolMenu.pid = 0;
	$.fn.schoolMenu.pname = "";
	// 学校id
	$.fn.schoolMenu.sid = 0;
	$.fn.schoolMenu.schoolName = "";
	// 单个选中对象
	$.fn.schoolMenu.$input = "";

	$.fn.schoolMenu.init = function() {
		var t = this;
		// 创建或找到menu主面板
		t.$schoolMenu = $("#ffui-school-menu");
		if (t.$schoolMenu.size() == 0) {
			t.$schoolMenu = $('<div id="ffui-school-menu" class="ffui-school-menu" ><div class="ffui-school-menu-p" ></div><div class="ffui-school-menu-c" ></div></div>');
			$("body").append(t.$schoolMenu);
			// 载入省份数据
			t.loadProvince(function(jsonArr) {
						t.createProvince(jsonArr);
					});

			// 关闭
			$(document).click(function(e) {
						t.$schoolMenu.hide();
						t.defaults.closeFn();
					});
			t.$schoolMenu.click(function() {
						return false;
					});
		}

	};

	// 创建省份菜单
	$.fn.schoolMenu.createProvince = function(josnArr) {
		var t = this;
		var $p = t.$schoolMenu.find("div.ffui-school-menu-p")
				.addClass("remove-bg");
		var html = "";

		$(josnArr).each(function(i, json) {
			if (json.id == 37) {
				return false;
			}
			html += '<a href="#" id="ffui-school-menu-p_' + json.id + '" >'
					+ json.name + '</a>';
		});

		$p.html(html);
		$p.find("a").click(function() {
			t.pid = $(this).attr("id").split("_")[1];
			t.pname = $(this).html();
			// 重置城市名
			t.sid = 0;
			t.schoolName = "";

			$p.find("a").removeClass("here");
			$(this).addClass("here");

			// 显示进度条
			t.$schoolMenu.find("div.ffui-school-menu-c")
					.removeClass("remove-bg");

			t.loadSchool(t.pid, function(jsonArr) {
						t.createSchool(jsonArr);
					});
			return false;
		});
	};

	// 创建学校
	$.fn.schoolMenu.createSchool = function(jsonArr) {
		var t = this;
		var $c = t.$schoolMenu.find("div.ffui-school-menu-c")
				.addClass("remove-bg");
		var html = "";
		$(jsonArr).each(function(i, json) {
			html += '<a href="#" id="ffui-school-menu-c_'
					+ json.id
					+ '" title="'
					+ json.name
					+ '" >'
					+ (json.name.length > 12 ? json.name.substring(0, 12)
							+ ".." : json.name) + '</a>';
		});
		$c.html(html).show();
		$c.find("a").click(function() {
					t.sid = $(this).attr("id").split("_")[1];
					t.schoolName = $(this).attr("title");
					$c.find("a").removeClass("here");
					$(this).addClass("here");

					t.setInfo();

					t.$schoolMenu.hide();
					// $mask.hide();
					return false;
				});
	};
	// 设置显示信息
	$.fn.schoolMenu.setInfo = function() {
		var t = this;
		t.$input.val(t.schoolName);

		t.defaults.next(t.pid, t.pname, t.sid, t.schoolName);

	};

	$.fn.schoolMenu.showMenu = function(input) {
		var t = this;
		var offset = $(input).offset();

		t.$schoolMenu.css({
					top : offset.top + $(input).outerHeight(true),
					left : offset.left,
					display : "block"
				});
	};

	// 载入地区数据
	$.fn.schoolMenu.loadProvince = function(next) {
		$.get('/v2/js/ffui/data/province/province.js', function(data) {
					if (next) {
						var json = $.parseJSON(data);
						next(json);
					}
				});
	};
	// 载入学校数据
	$.fn.schoolMenu.loadSchool = function(id, next) {
		$.get('/v2/js/ffui/data/dx/dx_' + id + ".js?", function(data) {
					if (next) {
						var json = $.parseJSON(data);
						next(json);
					}
				});
	};

})(jQuery);

/**
 * 
 */
$.fn.extend({
			/**
			 * 在窗口中居中显示某对象
			 */
			centerShow : function() {
				var $obj = $(this);
				var width = $obj.outerWidth(true);
				var height = $obj.outerHeight(true);
				if ($.browser.msie && $.browser.version == "6.0") {
					$obj.css({
								"position" : "absolute",
								"display" : "block",
								visibility : "visible"
							});
					var ie6Style = function() {
						$obj.css({
									"left" : Math
											.floor(($(window).width() - width)
													/ 2),
									"top" : Math
											.floor(($(window).height() - height)
													/ 2)
											+ $(window).scrollTop()
								});
					};
					ie6Style();
					$(window).resize(function() {
								ie6Style();
							}).scroll(function() {
								ie6Style();
							});
				} else {
					var $centerBox = $("#ffui-center-box");

					if ($centerBox.size() == 0) {
						$centerBox = $("<div id='ffui-center-box' />");
						$centerBox.css({
									"position" : "fixed",
									"top" : "50%",
									"left" : "50%",
									"z-index" : 199
								});
						$("body").append($centerBox);
					}
					if ($obj.size() > 1) {
						$centerBox.find("#" + $obj.eq(0).attr("id")).remove();
					}

					$centerBox.append($obj);
					$obj.css({
								"position" : "absolute",
								"left" : -Math.floor(width / 2),
								"top" : -Math.floor(height / 2),
								"display" : "block",
								visibility : "visible"
							});
				}
				return this;
			},
			/**
			 * 将对象固定在窗口中某个位置
			 * 
			 * @param {}
			 *            css
			 * @return {}
			 */
			posShow : function(css) {
				var obj = this;
				if ($.browser.msie && $.browser.version == "6.0") {
					$.extend(css, {
								"position" : "absolute",
								"display" : "block"
							});

					var tempCss = {};
					$.extend(tempCss, css);
					var height = $(obj).height();
					var setPos = function() {
						if (css["bottom"] != "undefined") {
							tempCss["bottom"] = parseInt(css["bottom"]);
							tempCss["zoom"] = 1;
							$(obj).css(tempCss);
							tempCss["zoom"] = 0;
						}
						$(obj).css(tempCss);
					};

					setPos();

					$(window).scroll(function() {
								setPos();
							}).resize(function() {
								setPos();
							});

				} else {
					$.extend(css, {
								"position" : "fixed",
								"display" : "block"
							});
					$(obj).css(css);
				}

				return this;
			},
			// 延迟关闭
			lazyHide : function(time) {
				var $obj = $(this);
				setTimeout(function() {
							$obj.fadeOut("slow");
						}, time);
				return this;
			},
			// 文本框文字计数器
			wordCount : function(options) {
				var o = {
					tipId : "tip",
					max : 50,
					maxClass : "maxClass"

				};
				$.extend(o, options);
				var $obj = $(this);
				var $tip = $("#" + o.tipId);
				var countByteLength = function(str, cnCharByteLen) {
					var byteLen = 0;

					for (var i = 0; i < str.length; i++) {
						if (/[\x00-\xff]/.test(str.charAt(i))) {
							byteLen += 1;
						} else {
							byteLen += cnCharByteLen;
						}
					}
					// $("body").append(str + "===" + str.length + ",<br/>");
					return Math.ceil(byteLen / 2);
					// return byteLen;
				};

				var count = 0;

				
				// onpropertychange
				$obj.bind("keyup blur focus paste cut", function() {
							var str = $obj.val();
							count = countByteLength($.trim(str), 2);
							if (count > o.max) {
								var extraLen = count - o.max;
								var extraChar = extraLen * 2;
								var byteLen = 0;
								var i = str.length;
								for (; i >= 0 && extraChar > 0; i--) {
									if (/[\x00-\xff]/.test(str.charAt(i))) {
										extraChar -= 1;
									} else {
										extraChar -= 2;
									}
								}
								$obj.val(str.substring(0, i));
							} else {
								$tip.html(count);
							}
						});
			}
		});
